//历史订单的批量操作
import { Space, message } from "antd";
import { useState } from "react";
import SelectActions from "../../../../common/components/SelectAction";
import ActionMap from "../ActionMap";
import { checkOrder, countOrderSum } from "../utils/checkOrderConfig";
import { updateOrderStatus } from "../utils/orderStatus";

//默认样式
const defaultStyle = {
	display: 'inline-block',
	margin: '0 8px 8px 0'
}
const Actions = props => {
	const { operationPage, searchForm, selectKeys, orderList, handleOk } = props;
	const [modalName, setModalName] = useState();
	const [item, setItem] = useState({});

	//智能操作，不需要勾选
	const handleAutoOpera = props => {
		const { scene, modalValue, item = {} } = props;
		setItem({
			scene,
			...item
		});
		setModalName(modalValue);
	};

	//点击批量更新状态
	const handleUpdateOrderStatus = props => {
		const { scene, isModal, modalValue, isCheck = true, item = {} } = props;
		//这里需要判断是否选择了数据
		if (selectKeys.length > 0) {
			let checkResult = false,
				countList = [];
			if (isCheck) {
				checkResult = checkOrder(scene, selectKeys, orderList);
				countList = countOrderSum(scene, selectKeys, orderList);
			}

			if (checkResult || !isCheck) {
				if (isModal) {
					setItem({
						scene,
						countList,
						...item
					});
					setModalName(modalValue);
				} else {
					const items = {
						selectKeys,
						orderList
					};
					updateOrderStatus(scene, items, afterUpdate);
				}
			}
		} else {
			message.info("请选择订单");
		}
	};

	//更新状态之后的函数
	const afterUpdate = () => {
		//这里处理成功的逻辑
	};

	//弹框参数
	const ModalProps = {
		item,
		searchForm,
		selectKeys,
		orderList,
		operationPage,
		handleOk(updateType, data, paymentStatistics) {
			afterUpdate();
			setModalName();
			handleOk(updateType, data, paymentStatistics);
		},
		handleCancel() {
			setModalName();
			setItem({});
		}
	};

	return (
		<>
			<ActionMap
				modalName={modalName}
				onCancel={() => {
					setModalName("");
				}}
				ModalProps={ModalProps}
			/>
			<SelectActions
				name="导出订单"
				style={defaultStyle}
				options={[
					{
						label: "按勾选订单导出",
						action() {
							handleUpdateOrderStatus({
								isModal: true,
								isCheck: false,
								item: { isBatch: true, taskType: 2002 },
								modalValue: 'ExportTelCodeModal'
							});
						}
					},
					{
						label: "按查询订单导出",
						action() {
							if (orderList.length) {
								handleAutoOpera({
									item: { isBatch: false, taskType: 2002 },
									modalValue: 'ExportTelCodeModal'
								});
							} else {
								message.info("暂无可导出的数据！");
							}
						}
					},
					{
						label: "维护导出表字段信息",
						action() {
							// handleUpdateOrderStatus({ isModal: true, isCheck: false, modalValue: ExportOrderExcelModal });
							setModalName('ExportOrderExcelModal');
						}
					}
				]}
			/>
		</>
	);
};
export default Actions;
